/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.st.web;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.st.config.Constant;
import com.jeesite.modules.st.entity.StMaterialRecord;
import com.jeesite.modules.st.entity.StProductMaterialFormBill;
import com.jeesite.modules.st.entity.StSmallMaterialApprove;
import com.jeesite.modules.st.entity.StWarehouseInventory;
import com.jeesite.modules.st.pojo.AjaxJson;
import com.jeesite.modules.st.service.StMaterialRecordService;
import com.jeesite.modules.st.service.StProductMaterialFormBillService;
import com.jeesite.modules.st.service.StSmallMaterialApproveService;
import com.jeesite.modules.st.service.StWarehouseInventoryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 生产领料Controller
 *
 * @author HOHD
 * @version 2020-12-22
 */
@Controller
@RequestMapping(value = "${adminPath}/st/stProductMaterialFormBill")
public class StProductMaterialFormBillController extends BaseController {

    @Autowired
    private StProductMaterialFormBillService stProductMaterialFormBillService;

    /**
     * 获取数据
     */
    @ModelAttribute
    public StProductMaterialFormBill get(String id, boolean isNewRecord) {
        return stProductMaterialFormBillService.get(id, isNewRecord);
    }

    /**
     * 查询列表
     */
    @RequiresPermissions("st:stProductMaterialFormBill:view")
    @RequestMapping(value = {"list", ""})
    public String list(StProductMaterialFormBill stProductMaterialFormBill, Model model) {
        model.addAttribute("stProductMaterialFormBill", stProductMaterialFormBill);
        return "modules/st/stProductMaterialFormBillList";
    }

    @RequiresPermissions("st:stProductMaterialFormBill:view")
    @RequestMapping(value = {"scanView"})
    public String scanView(String needNumber, String warehouseNumber, String left, Model model) {
        Map<String, Object> bean = new HashMap<>();
        bean.put("needNumber", new BigDecimal(needNumber).subtract(new BigDecimal(left)));
        bean.put("warehouseNumber", warehouseNumber);
        model.addAttribute("bean", bean);
        return "modules/st/stProductMaterialFormBillForm-scan-view";
    }

    @Autowired
    StWarehouseInventoryService stWarehouseInventoryService;

    @RequiresPermissions("st:stProductMaterialFormBill:view")
    @RequestMapping(value = {"scanView0"})
    public String scanView0(StWarehouseInventory stWarehouseInventory, String productCode, Model model) {
//		List<Map<String, Object>> list = new ArrayList<>();
//		Map<String, Object> map1 = new HashMap<>();
//		map1.put("warehouseName","原材料仓");
//		map1.put("id",1);
//		map1.put("warehouseNumber",3000);
//		Map<String, Object> map2 = new HashMap<>();
//		map2.put("warehouseName","明矾仓");
//		map2.put("id",2);
//		map2.put("warehouseNumber",1500);
//		list.add(map1);
//		list.add(map2);
        String batchNumber = stWarehouseInventory.getBatchNumber();
//        String materialName = stWarehouseInventory.getMaterialName();//code 转化为id
        String materialCode = stWarehouseInventory.getMaterialCode();
        String code = null;
        int goodsType = 0;
        //两个东西，有且只有一个有值
        if (null == materialCode) {
            code = productCode;
            goodsType = Constant.warehouse_goods_type.complete_product;
        } else {
            code = materialCode;
            goodsType = Constant.warehouse_goods_type.material;
        }
        List<StWarehouseInventory> list = stWarehouseInventoryService.getGoodsNumber(stWarehouseInventory);
        model.addAttribute("list", list);
        return "modules/st/stProductMaterialFormBillForm-scan-view-0";
    }

    /**
     * 查询列表数据
     */
    @RequiresPermissions("st:stProductMaterialFormBill:view")
    @RequestMapping(value = "listData")
    @ResponseBody
    public Page<StProductMaterialFormBill> listData(StProductMaterialFormBill stProductMaterialFormBill, HttpServletRequest request, HttpServletResponse response) {
        stProductMaterialFormBill.setPage(new Page<>(request, response));
        Page<StProductMaterialFormBill> page = stProductMaterialFormBillService.findPage(stProductMaterialFormBill);
        return page;
    }

    @Autowired
    StSmallMaterialApproveService stSmallMaterialApproveService;

    @RequiresPermissions("st:stProductMaterialFormBill:view")
    @RequestMapping(value = "canNew")
    @ResponseBody
    public AjaxJson canNew() {//能否new
        AjaxJson<String> ajaxJson = new AjaxJson<>();
        StSmallMaterialApprove query = new StSmallMaterialApprove();
        query.setTakeStatus(Constant.take_status.in_take);
        List<StSmallMaterialApprove> list = stSmallMaterialApproveService.findList(query);
        if (!list.isEmpty()) {
            ajaxJson.code = -1;
            ajaxJson.data = "有未审核的料";
            return ajaxJson;
        }
        return ajaxJson;
    }

    @Autowired
    StMaterialRecordService stMaterialRecordService;
    ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 查看编辑表单
     */
    @RequiresPermissions("st:stProductMaterialFormBill:view")
    @RequestMapping(value = "form")
    public String form(StProductMaterialFormBill stProductMaterialFormBill, Model model) throws JsonProcessingException {
        Map<String, Object> map = new HashMap<>();
        List<StMaterialRecord> list = stMaterialRecordService.findAll(map);
        model.addAttribute("materialRecordList", objectMapper.writeValueAsString(list));
        model.addAttribute("stProductMaterialFormBill", stProductMaterialFormBill);
        return "modules/st/stProductMaterialFormBillForm";
    }

    /**
     * 保存数据
     */
    @RequiresPermissions("st:stProductMaterialFormBill:edit")
    @PostMapping(value = "save")
    @ResponseBody
    public String save(@Validated StProductMaterialFormBill stProductMaterialFormBill) {
        stProductMaterialFormBillService.save(stProductMaterialFormBill);
        stProductMaterialFormBill.setStProductMaterialFormBillDetailList(null);
        return renderResult(Global.TRUE, text("保存生产领料成功！"), stProductMaterialFormBill);
    }

    /**
     * 删除数据
     */
    @RequiresPermissions("st:stProductMaterialFormBill:edit")
    @RequestMapping(value = "delete")
    @ResponseBody
    public String delete(StProductMaterialFormBill stProductMaterialFormBill) {
        stProductMaterialFormBillService.delete(stProductMaterialFormBill);
        return renderResult(Global.TRUE, text("删除生产领料成功！"));
    }

}